<?php

$user_obj = new user_obj();
$filter_obj = new filter_obj();

if (isset($_REQUEST['action'])) {
    switch ($_REQUEST['action']) {
        case 'login':
            $username = strip_tags(htmlspecialchars($_REQUEST['username']));
            $password = strip_tags(htmlspecialchars($_REQUEST['password']));

            if (empty($username) || empty($password)) {
                error_obj::setError(new error_obj('$username or $password is empty', __FILE__, "login"), EMPTY_LOGIN);
                unset($_SESSION['user']);
            } else {
                $user = $user_obj->login($username, $password);
                if (error_obj::isError($user)) {
                    error_obj::setError($user, UNEXCEPTED);
                    unset($_SESSION['user']);
                    return false;
                }
                if (empty($user)) {
                    error_obj::setError(new error_obj('$user is empty', __FILE__, "login"), BAD_LOGIN);
                    unset($_SESSION['user']);
                    return false;
                } else {
                    $_SESSION['timeout'] = time();
                    $_SESSION['user'] = $user;
                    $_SESSION['user']['user_area_id'] = $user_obj->getUserArea($_SESSION['user']['user_type_id']);
                    $_SESSION['user']['profile'] = $user_obj->getUserById($_SESSION['user']['user_id']);
                    
                    $pass = $_SESSION['user']['profile']['password'];
                    foreach ($_SESSION['user']['profile'] as $key => $value) {
                        if ($value == $pass){
                            unset($_SESSION['user']['profile'][$key]);
                            unset($_SESSION['user']['profile']['password']);
                            unset($pass);
                            break;
                        }
                    }
                    
                    $filters = $filter_obj->getFilters($_SESSION['user']['user_id']);
                    if (!error_obj::isError($filters)){
                        foreach ($filters as $key => $filter) {
                            $_SESSION[$filter['variable']] = json_decode($filter['filter'],true);
                        }
                    }
                    /*$user_options = $user_obj->getUserOptions($_SESSION['user']['user_id']);
                    if (error_obj::isError($user_options))
                        error_obj::setError(UNEXCEPTED, $user_options);
                    else
                        if (!empty($user_options)){
                            $_SESSION['user']['rememberClientFilter'] = $user_options['clientFilter'];
                            if ($user_options['clientFilter']!=0){
                                $_SESSION['user']['clientFilter'] = $user_options['clientFilter'];
                                $filter_obj->setClientToEachFilter($_SESSION['user']['clientFilter']);
                            } 
                        } 
                    */
                    if ($_SESSION['user']['profile']['userlevel'] != USER_TYPE_CLIENT){
                        $client_obj = new client_obj();
                        
                        $clientByIp = $client_obj->getClientByIP($_SERVER['REMOTE_ADDR']);
                        if (!error_obj::isError($clientByIp) && !empty($clientByIp)){
                            $_SESSION['user']['clientFilter'] = $clientByIp;
                            $filter_obj->setClientToEachFilter($clientByIp);
                        }
                            
                    }   
                        
                    if ($_SESSION['user']['profile']['userlevel']==USER_TYPE_TECHNIC){
                        if (!isset($_SESSION['list_of_tasks_filter']['status'])){
                            $_SESSION['list_of_tasks_filter']['status'] = 1;
                        }
                        if (!isset($_SESSION['list_of_tasks_filter']['responsible_or_worker'])){
                            $_SESSION['list_of_tasks_filter']['responsible_or_worker'] = $_SESSION['user']['user_id'];
                        }
                    }
                    if ($_SESSION['user']['profile']['userlevel']==USER_TYPE_CLIENT){
                        $_SESSION['user']['clients'] = array();
                        $_SESSION['user']['clients'][$_SESSION['user']['profile']['client']] = $_SESSION['user']['profile']['client'];
                        $associatedClients = $user_obj->getAssociatedClientsByUserId($_SESSION['user']['user_id']);
                        if (!error_obj::isError($associatedClients) && !empty($associatedClients)){
                            foreach ($associatedClients as $k => $id) {
                                $_SESSION['user']['clients'][$id] = $id;
                            }
                        }
                    }
                    
                    $filter_obj->setDefaultFilterOfListToValue('list_of_tasks_filter', 'status', 1);
                    $filter_obj->setDefaultFilterOfListToValue('timesheets_filter', 'year', date("Y"));
                    
                    if (!empty($_REQUEST['goToTask']))
                        base_obj::redirect(ROOT . '?input=edit_task&task=' . $_REQUEST['goToTask']);
                    
                    if (!empty($_REQUEST['goToTsdr']))
                        base_obj::redirect(ROOT . '?input=tsdr_more&tsdr_id=' . $_REQUEST['goToTsdr']);
                    
                    header("Location: " . ROOT . '?input=main');
                    exit;
                }
            }

            break;
    }
}

if (isset($_REQUEST['toTask']) && is_numeric($_REQUEST['toTask'])){
    $GLOBALS['template_data']['toTask'] = (int) $_REQUEST['toTask'];
}

if (isset($_REQUEST['toTsdr']) && is_numeric($_REQUEST['toTsdr'])){
    $GLOBALS['template_data']['toTsdr'] = (int) $_REQUEST['toTsdr'];
}
?>
